//
//  XLMainViewController.swift
//  SinaWeibo
//
//  Created by 李凯 on 16/8/29.
//  Copyright © 2016年 kevin. All rights reserved.
//

import UIKit

class XLMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //自定义tabbar 通过kvc赋值
        let tabBar = XLTabBar()
        self.setValue(tabBar, forKey: "tabBar")
        
        //定义一个闭包
        let closure = {[weak self] () -> () in
            print("撰写按钮点击 请弹出菜单 \(self)")
            let v = XLComposeView()
            v.show(target: self!)
            
        }
        //将闭包传给tabbar
        tabBar.composeBtnClosure = closure
        
        
        addChildViewController()
        
    }
    
    func addChildViewController() {
        addChildViewController(vc: XLHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(vc: XLMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(vc: XLDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(vc: XLProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }

    func addChildViewController(vc: UITableViewController, title: String,  imageName: String) {
        //设置标题
        vc.title = title
        //设置默认图片
        vc.tabBarItem.image = UIImage(named: imageName)
        //设置选中图片
        vc.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")
        //设置选中的字体颜色 默认为黑色, 不用设置
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .selected)
        if(vc.title == "消息")
        {
            vc.tabBarItem.badgeValue = "23"
        }
        //嵌套到导航控制器
        let nav = XLNavigationController(rootViewController: vc)
        //添加到tabbarcontroller
        addChildViewController(nav)
        
        
    }
    

  

}
